use anyhow::anyhow;
use salvo::{Depot, Request};

pub struct RoleService {}

impl RoleService {
    pub async fn add_role(_request: &mut Request, _depot: &mut Depot) -> anyhow::Result<()> {
        /*let add_role_req = request.parse_json::<AddRoleReq>().await?;
        let sys_role = SysRole {
            role_id: None,
            role_name: add_role_req.role_name,
            parent_id: add_role_req.parent_id,
            create_time: Some(Local::now().format(DATE_FORMAT).to_string()),
            state: Some(1),
            org_id: Some(1),
        };
        let result = SysRole::insert(DB.get().unwrap(), &sys_role).await?;
        if let Some(menus) = add_role_req.menus {
            let sys_role_menus = menus
                .iter()
                .map(|f| SysRoleMenu {
                    role_menu_id: None,
                    role_id: Some(result.last_insert_id.as_i64().unwrap()),
                    menu_id: Some(*f),
                    org_id: None,
                    create_time: Some(Local::now().format(DATE_FORMAT).to_string()),
                })
                .collect::<Vec<SysRoleMenu>>();
            SysRoleMenu::insert_batch(
                DB.get().unwrap(),
                &sys_role_menus,
                sys_role_menus.len() as u64,
            )
                .await?;
        }*/
        Ok(())
    }

    pub async fn del_role(request: &mut Request, _depot: &mut Depot) -> anyhow::Result<()> {
        let role = request
            .query::<i64>("role_id")
            .ok_or(anyhow!("role not found"))?;
        //TODO  if some user using role   stop delete;
        /* let _ = SysRole::delete_by_column(DB.get().unwrap(), "role_id", role).await;*/
        Ok(())
    }
}
